using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using CodeSnippet.Config;
using CodeSnippet.Formats;
using CodeSnippet.Formats.Base;
using CodeSnippet.Helpers;
using CodeSnippet.Properties;
using WLWPluginBase.Win32;

namespace CodeSnippet.Forms
{
    /// <summary>
    /// Code Snippet main form.
    /// </summary>
    internal partial class CodeSnippetForm : Form, ICodeSnippetForm
    {
        #region Fields
        /// <summary>
        /// Flag used as a semaphore indicating whether or not this dialog is initialized.
        /// </summary>
        private bool isInitialized;
        #endregion Fields

        #region Properties
        /// <summary>
        /// Gets the readme file location.
        /// </summary>
        private static string ReadmeFile
        {
            get { return ((Assembly.GetExecutingAssembly().Location) == null ? string.Empty : Assembly.GetExecutingAssembly().Location.Replace(".dll", ".rtf")); }
        }
        /// <summary>
        /// Plugin configuration.
        /// </summary>
        public CodeSnippetConfig Config { get; set; }
        /// <summary>
        /// Code snippet contents processed and ready for return as new content.
        /// </summary>
        public string CodeSnippet { get; private set; }
        /// <summary>
        /// Code snippet content to be processed.
        /// </summary>
        /// <param name="value"></param>
        public string CodeSnippetToEdit
        {
            set { rtbCodeSnippet.Text = value; }
        }
        #endregion Properties

        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeSnippetForm"/> class.
        /// </summary>
        /// <param name="config">The configuration object.</param>
        public CodeSnippetForm(CodeSnippetConfig config)
        {
            // Perform first step initialization.
            CodeSnippet = string.Empty;
            Config = config;
            InitializeComponent();
            InitializeList();
            // Proceed to allowing the rest of the UI to be updated.
            ConfigToUI();
        }
        /// <summary>
        /// Initializes the list of supported languages.
        /// </summary>
        private void InitializeList()
        {
            // Load the list of all supported formats (languages).
            if (cbLang.ComboBox != null)
            {
                cbLang.ComboBox.DataSource = SupportedFormat.Items;
            }
        }
        #endregion Initialization

        #region Format Operations
        /// <summary>
        /// Apply the configuration to the UI.
        /// </summary>
        private void ConfigToUI()
        {
            miFileRunSilent.Checked = Config.General.RunSilent;
            cbLang.SelectedItem = SupportedFormat.GetItem(Config.General.ActiveLanguage);
            miEditTrimOnPaste.Checked = rtbCodeSnippet.TrimIndentOnPaste = Config.Editor.TrimIndentOnPaste;
            miViewWrap.Checked = btnWordWrap.Checked = rtbCodeSnippet.WordWrap = Config.Editor.WordWrap;
            miFormatEmbedStyles.Checked = btnEmbedStyles.Checked = Config.Style.EmbedStyles;
            rtbCodeSnippet.TabSpaces = Config.Editor.TabSpaces;
            miFormatUseContainer.Checked = btnUseContainer.Checked = Config.Style.UseContainer;
            miFormatLineNumbers.Checked = btnLineNumbers.Checked = Config.Style.LineNumbers;
            miFormatAlternateLines.Checked = btnAlternateLines.Checked = Config.Style.AlternateLines;
            ShowCodePane(Config.General.ActiveView);
        }
        /// <summary>
        /// Apply the UIs configuration changes.
        /// </summary>
        /// <param name="isPreview">flag indicating whether or not to update the preview pane.</param>
        /// <returns>Updated configuration object.</returns>
        private CodeSnippetConfig UIToConfig(bool isPreview)
        {
            // Initialize a new instance of the configuration based on the current UI settings.
            var c = new CodeSnippetConfig(Config)
                        {
                            General =
                                {
                                    ActiveLanguage = SupportedFormat.GetItemKey(cbLang.SelectedItem),
                                },
                            Layout =
                                {
                                    FullModePlacement = Win32WndHelper.GetPlacement(Handle)
                                },
                            Style =
                                {
                                    AlternateLines = btnAlternateLines.Checked,
                                    EmbedStyles = (isPreview ? true : btnEmbedStyles.Checked),
                                    LineNumbers = btnLineNumbers.Checked,
                                    UseContainer = btnUseContainer.Checked,
                                }
                        };
            return c;
        }
        #endregion Format Operations

        #region Event Handlers

        #region Form Events
        /// <summary>
        /// Handles the Load event of the CodeSnippetForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CodeSnippetForm_Load(object sender, EventArgs e)
        {
            // Determine if we need to reposition the window based on stored placement.
            if (Config.Reposition)
            {
                Win32WndHelper.SetPlacement(Handle, Config.Layout.FullModePlacement);
            }
            // Indicate we have now being initialized.
            isInitialized = true;
            // Now we can force a format of the code. We do this since the flag isInitilized
            // will prevent the formatting to occur, so as to avoid problems with overwriting
            // properties.
            FormatCode(true);
        }
        /// <summary>
        /// Handles the FormClosing event of the CodeSnippetForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void CodeSnippetForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Config = UIToConfig(false);
        }
        /// <summary>
        /// Handles the Activated event of the CodeSnippetForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CodeSnippetForm_Activated(object sender, EventArgs e)
        {
            UpdateButtonStates();
            rtbCodeSnippet.Focus();
        }
        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            FormatCode(false);
        }
        #endregion Form Events

        #region Web Browser Events
        /// <summary>
        /// Handles the DocumentCompleted event of the wbPreview control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.WebBrowserDocumentCompletedEventArgs"/> instance containing the event data.</param>
        private void wbPreview_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Determine if this is the first time we are updating the web preview. If so, check to see if
            // we have some content in the rich text box control. When the form is first loaded and we are
            // being fed source code coming from the Windows Live Writer editor window we will be setting
            // the contents of the rich text box control. In all other cases this condition will not result
            // in any processing.
            if (!string.IsNullOrEmpty(rtbCodeSnippet.Text) && string.IsNullOrEmpty(wbPreview.DocumentText))
            {
                FormatCode(true);
            }
        }
        /// <summary>
        /// Handles the PreviewKeyDown event of the wbPreview control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.PreviewKeyDownEventArgs"/> instance containing the event data.</param>
        private void wbPreview_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            // The WebBrowser control does not process the keys because of the way in
            // which we are using it. Therefore, we will process them here whenever the
            // control has the focus.
            if (!e.Control)
            {
                return;
            }
            switch (e.KeyCode)
            {
                case Keys.D1:
                    miViewCodeSnippet_Click(sender, e);
                    break;
                case Keys.D2:
                    miViewFormattedCodeSnippet_Click(sender, e);
                    break;
                case Keys.D3:
                    miViewBoth_Click(sender, e);
                    break;
                case Keys.A:
                    miEditSelectAll_Click(sender, e);
                    break;
                case Keys.V:
                    miEditPaste_Click(sender, e);
                    break;
            }
        }
        #endregion Web Browser Events

        #region File Events
        /// <summary>
        /// Handles the Click event of the miFileRunSilent control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miFileRunSilent_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show(this, Resources.RunningSilent, Text,
                                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,
                                                    CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : 0))
            {
                Config.General.RunSilent = miFileRunSilent.Checked;
                Close();
            }
            else
            {
                miFileRunSilent.Checked = false;
            }
        }
        /// <summary>
        /// Handles the Click event of the miFileClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miFileClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        #endregion File Events

        #region Edit Events
        /// <summary>
        /// Handles the SelectionChanged event of the rtbCodeSnippet control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rtbCodeSnippet_SelectionChanged(object sender, EventArgs e)
        {
            UpdateButtonStates();
        }
        /// <summary>
        /// Handles the Click event of the miEditUndo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditUndo_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.Undo();
        }
        /// <summary>
        /// Handles the Click event of the miEditRedo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditRedo_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.Redo();
        }
        /// <summary>
        /// Handles the Click event of the miEditCut control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditCut_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.Cut();
            UpdateButtonStates();
        }
        /// <summary>
        /// Handles the Click event of the miEditCopy control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditCopy_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.Copy();
        }
        /// <summary>
        /// Handles the Click event of the miEditPaste control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditPaste_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.Paste();
            UpdateButtonStates();
        }
        /// <summary>
        /// Handles the Click event of the miEditClearClipboard control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditClearClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            UpdateButtonStates();
        }
        /// <summary>
        /// Handles the Click event of the miEditSelectAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditSelectAll_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.SelectAll();
        }
        /// <summary>
        /// Handles the Click event of the miEditLineIndentDecrease control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditLineIndentDecrease_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.DecIndent();
        }
        /// <summary>
        /// Handles the Click event of the miEditLineIndentIncrease control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditLineIndentIncrease_Click(object sender, EventArgs e)
        {
            rtbCodeSnippet.IncIndent();
        }
        /// <summary>
        /// Handles the Click event of the miEditTrimIndentOnPaste control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miEditTrimIndentOnPaste_Click(object sender, EventArgs e)
        {
            Config.Editor.TrimIndentOnPaste = rtbCodeSnippet.TrimIndentOnPaste = miEditTrimOnPaste.Checked;
        }
        #endregion Edit Events

        #region View Events
        /// <summary>
        /// Handles the Click event of the miViewCompactMode control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miViewCompactMode_Click(object sender, EventArgs e)
        {
            Config.General.ActiveMode = CodeSnippetViewMode.Compact;
            DialogResult = DialogResult.Retry;
            Close();
        }
        /// <summary>
        /// Handles the Click event of the miViewWrap control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miViewWrap_Click(object sender, EventArgs e)
        {
            Config.Editor.WordWrap = rtbCodeSnippet.WordWrap = btnWordWrap.Checked = miViewWrap.Checked;
        }
        /// <summary>
        /// Handles the Click event of the btnWordWrap control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnWordWrap_Click(object sender, EventArgs e)
        {
            Config.Editor.WordWrap = rtbCodeSnippet.WordWrap = miViewWrap.Checked = btnWordWrap.Checked;
        }
        /// <summary>
        /// Handles the Click event of the miViewCodeSnippet control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miViewCodeSnippet_Click(object sender, EventArgs e)
        {
            ShowCodePane(CodeSnippetViewType.CodeSnippet);
        }
        /// <summary>
        /// Handles the Click event of the miViewFormattedCodeSnippet control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miViewFormattedCodeSnippet_Click(object sender, EventArgs e)
        {
            ShowCodePane(CodeSnippetViewType.FormattedCodeSnippet);
        }
        /// <summary>
        /// Handles the Click event of the miViewBoth control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miViewBoth_Click(object sender, EventArgs e)
        {
            ShowCodePane(CodeSnippetViewType.Both);
        }
        /// <summary>
        /// Handles the Click event of the btnViewHtml control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnViewHtml_Click(object sender, EventArgs e)
        {
            Win32IEHelper.ViewSource(wbPreview.Handle);
        }
        #endregion View Events

        #region Format Events
        /// <summary>
        /// Reformats the code.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ReformatCode(object sender, EventArgs e)
        {
            // Ensure we have been correctly initialized before attempting to reformat the code.
            if (isInitialized)
            {
                FormatCode(true);
            }
        }
        /// <summary>
        /// Handles the Click event of the miFormatLineNumbers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miFormatLineNumbers_Click(object sender, EventArgs e)
        {
            btnLineNumbers.Checked = miFormatLineNumbers.Checked;
            FormatCode(true);
        }
        /// <summary>
        /// Handles the Click event of the btnLineNumbers control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnLineNumbers_Click(object sender, EventArgs e)
        {
            miFormatLineNumbers.Checked = btnLineNumbers.Checked;
            FormatCode(true);
        }
        /// <summary>
        /// Handles the Click event of the miFormatAlternateLines control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miFormatAlternateLines_Click(object sender, EventArgs e)
        {
            btnAlternateLines.Checked = miFormatAlternateLines.Checked;
            FormatCode(true);
        }
        /// <summary>
        /// Handles the Click event of the btnAlternateLineBkg control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAlternateLineBkg_Click(object sender, EventArgs e)
        {
            miFormatAlternateLines.Checked = btnAlternateLines.Checked;
            FormatCode(true);
        }
        /// <summary>
        /// Handles the Click event of the miFormatEmbedStyles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miFormatEmbedStyles_Click(object sender, EventArgs e)
        {
            btnEmbedStyles.Checked = miFormatEmbedStyles.Checked;
        }
        /// <summary>
        /// Handles the Click event of the btnEmbedStyles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnEmbedStyles_Click(object sender, EventArgs e)
        {
            miFormatEmbedStyles.Checked = btnEmbedStyles.Checked;
        }
        /// <summary>
        /// Handles the Click event of the miFormatUseContainer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miFormatUseContainer_Click(object sender, EventArgs e)
        {
            btnUseContainer.Checked = miFormatUseContainer.Checked;
            FormatCode(true);
        }
        /// <summary>
        /// Handles the Click event of the btnUseContainer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnUseContainer_Click(object sender, EventArgs e)
        {
            miFormatUseContainer.Checked = btnUseContainer.Checked;
            FormatCode(true);
        }
        /// <summary>
        /// Handles the Click event of the miCopyStylesToClipboard control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miCopyStylesToClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(SourceFormat.CssString);
            MessageBox.Show(this, Resources.CopyStyles, Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                            CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : 0);
        }
        #endregion Format Events

        #region Tools Events
        /// <summary>
        /// Handles the Click event of the miToolsOptions control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miToolsOptions_Click(object sender, EventArgs e)
        {
            // Make sure to get the latest configuration information from the UI controls.
            Config = UIToConfig(false);
            // Prepare a new instance of the configuration dialog.
            var dlg = new CodeSnippetOptionsForm(Config);
            if (DialogResult.OK == dlg.ShowDialog(this))
            {
                // Update the main forms configuration copy.
                Config = dlg.Config;
                // Force the main form to update with the new settings.
                ConfigToUI();
            }
            else
            {
                // Update only the options dialog placement information.
                Config.Layout.OptionsPlacement = dlg.Config.Layout.OptionsPlacement;
            }
        }
        #endregion Tools Events

        #region Help Events
        /// <summary>
        /// Handles the Click event of the miHelpReadme control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miHelpReadme_Click(object sender, EventArgs e)
        {
            try
            {
                Process.Start(ReadmeFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
                                CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ? MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading : 0);
            }
        }
        /// <summary>
        /// Handles the Click event of the miHelpVisitMyBlog control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miHelpVisitMyBlog_Click(object sender, EventArgs e)
        {
            Process.Start("http://lvildosola.blogspot.com");
        }
        /// <summary>
        /// Handles the Click event of the miHelpAbout control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void miHelpAbout_Click(object sender, EventArgs e)
        {
            new CodeSnippetAboutForm().ShowDialog(this);
        }
        #endregion Help Events

        #endregion Event Handlers

        #region Helpers
        /// <summary>
        /// Updates the button states.
        /// </summary>
        private void UpdateButtonStates()
        {
            miEditUndo.Enabled = btnUndo.Enabled = rtbCodeSnippet.CanUndo;
            miEditRedo.Enabled = btnRedo.Enabled = rtbCodeSnippet.CanRedo;
            miEditCut.Enabled = btnCut.Enabled = miEditCopy.Enabled = btnCopy.Enabled = miEditLineIndentDecrease.Enabled = btnDecIndent.Enabled =
                miEditLineIndentIncrease.Enabled = btnIncIndent.Enabled = rtbCodeSnippet.HasSelectedText;
            miEditPaste.Enabled = btnPaste.Enabled = miEditClearClipboard.Enabled = btnClipboardClear.Enabled = rtbCodeSnippet.CanPasteTextFromClipboard;
            miEditSelectAll.Enabled = btnViewHtml.Enabled = miViewHtml.Enabled = rtbCodeSnippet.HasText;
        }
        /// <summary>
        /// Shows the code pane.
        /// </summary>
        /// <param name="type">The view type.</param>
        private void ShowCodePane(CodeSnippetViewType type)
        {
            Config.General.ActiveView = type;
            switch(type)
            {
                case CodeSnippetViewType.CodeSnippet:
                    miViewFormattedCodeSnippet.Checked = miViewBoth.Checked = false;
                    sc1.Panel1Collapsed = false;
                    sc1.Panel2Collapsed = true;
                    break;
                case CodeSnippetViewType.FormattedCodeSnippet:
                    Config.General.ActiveView = CodeSnippetViewType.FormattedCodeSnippet;
                    miViewCodeSnippet.Checked = miViewBoth.Checked = false;
                    sc1.Panel1Collapsed = true;
                    sc1.Panel2Collapsed = false;
                    break;
                case CodeSnippetViewType.Both:
                    Config.General.ActiveView = CodeSnippetViewType.Both;
                    miViewCodeSnippet.Checked = miViewFormattedCodeSnippet.Checked = false;
                    sc1.Panel1Collapsed = false;
                    sc1.Panel2Collapsed = false;
                    break;
            }
        }
        /// <summary>
        /// Formats the code.
        /// </summary>
        /// <param name="isPreview">flag indicating whether or not to update the preview pane.</param>
        private void FormatCode(bool isPreview)
        {
            // Get the latest configuration information from the dialog.
            var config = UIToConfig(isPreview);
            CodeSnippet = FormatHelper.Format(SupportedFormat.GetItemKey(cbLang.SelectedItem),
                config.Editor, config.Style, rtbCodeSnippet.Text, rtbCodeSnippet.TrimIndentOnPaste);
            if (isPreview)
            {
                wbPreview.DocumentText = CodeSnippet;
            }
        }
        #endregion Helpers
    }
}